<template>
  <div>
    <el-form inline :model="queryData" ref="queryData" class="query-form">

      <el-form-item label="资源名称:" prop="name">
        <el-input v-model="queryData.name" placeholder="请输入" style="width: 200px" clearable
                  @clear="formClear('name')"/>
      </el-form-item>
      <el-form-item label="资源类型:" prop="type">
        <el-select v-model="queryData.type" placeholder="请选择" clearable @clear="formClear('type')"
                   style="width: 200px">
          <el-option :label="item.typeName" :value="item.type" v-for="(item, index) in resourceList" :key="index"/>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">查 询</el-button>
        <el-button @click="onReset">重 置</el-button>
        <el-button type="primary" @click="lookOverEvent(null)">新 增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableList" border style="width: 100%;">
      <el-table-column prop="id" label="资源ID" align="center" width="100"/>
      <el-table-column prop="name" label="资源名称" align="center"/>
      <el-table-column prop="code" label="资源CODE" align="center"/>
      <el-table-column prop="type" label="类型" align="center"/>
      <el-table-column prop="typeName" label="类型名称" align="center"/>
      <el-table-column prop="sort" label="排序" align="center"/>
      <el-table-column label="操作" align="center" width="150" fixed="right">
        <template slot-scope="{row}">
          <el-button type="text" @click="lookOverEvent(row)">查 看</el-button>
          <el-button type="text" @click="deleteEvent(row)">删 除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Paging v-model="queryData" @change="getRequest"></Paging>
    <el-drawer title="资源信息" :visible.sync="drawer" direction="rtl" :before-close="drawerClose" size="500px"
               destroy-on-close class="drawer-css">
      <StaticDataForm :entity="entity" v-if="drawer" @close="drawerClose" @refresh="getRequest"></StaticDataForm>
    </el-drawer>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'

  export default {
    computed: {
      ...mapGetters(['confirm'])
    },
    components: {
      StaticDataForm: () => import('./StaticDataForm.vue')
    },
    name: 'staticDataList',
    data() {
      return {
        tableList: [],
        resourceList: [],
        queryData: {
          name: null,
          type: null,
          pageNum: 1,
          pageSize: 10,
          total: 0
        },
        entity: null,
        drawer: false
      }
    },
    beforeMount() {
      this.getRequest()
      this.getResourceList()
    },
    methods: {
      deleteEvent(row) {
        this.$confirm(this.confirm.text, '提示', this.confirm.options).then(async() => {
          await this.$ajax.get(`/resource/table/remove?id=${row.id}`)
          this.$message.success('操作成功')
          this.onSubmit()
          this.getResourceList()
        })
      },
      /**
       * 新增 or 查看按钮事件
       */
      lookOverEvent (row) {
        this.entity = row ? JSON.parse(JSON.stringify(row)) : null
        this.drawer = true
      },
      /**
       * 关闭抽屉
       */
      drawerClose () {
        this.drawer = false;
        this.entity = null
        this.getResourceList()
      },
      /**
       * 重置事件
       */
      onReset() {
        this.$refs['queryData'].resetFields()
        this.getRequest()
      },
      /**
       * 查询条件提交事件
       */
      onSubmit() {
        this.queryData.pageNum = 1
        this.getRequest()
      },
      /**
       * 列表请求事件
       */
      async getRequest() {
        const params = JSON.parse(JSON.stringify(this.queryData))
        const res = await this.$ajax.get('/resource/table/list', { params })
        this.queryData.total = res.total
        this.tableList = res.rows
      },
      async getResourceList() {
        const res = await this.$ajax.get('/resource/table/typeNameList')
        this.resourceList = res.rows
      },
      /**
       * form-input 置空事件
       */
      formClear(key) {
        this.queryData[key] = null
        this.onSubmit()
      }
    }
  }
</script>

<style scoped>

</style>
